package com.cdbkj.cloud.pos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.androidyang.library.common.adapter.CommonAdapter;
import com.androidyang.library.common.adapter.Wrapper;
import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.bean.JsonResult;
import com.cdbkj.cloud.pos.utils.Tools;

import java.util.List;
import java.util.Map;

public class DoctorFragment extends Fragment implements AdapterView.OnItemClickListener, HttpClientHandler {
    private static final String ARG_PARAM1 = "userId";
    private static final String ARG_PARAM2 = "regDate";
    private static final String ARG_PARAM3 = "regTypeId";
    private static final String ARG_PARAM4 = "departmentId";
    private static final String ARG_PARAM5 = "departmentName";

    protected View rootView;
    protected ListView mListView;

    private String userId;
    private String regDate;
    private String regTypeId;
    private String departmentId;
    private String departmentName;
    private String scheduleCode;

    private ListAdapter adapter;
    private List<Map> list;

    private OnDoctorFragmentInteractionListener mListener;

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tools.dismissDialog();
            Tools.backgroundAlpha(getActivity(), 1);
            mListener.onDoctorFragmentJumpPreInteraction();
        }
    };

    public DoctorFragment() {
        // Required empty public constructor
    }

    public static DoctorFragment newInstance(String userId, String regDate, String regTypeId, String departmentId, String departmentName) {
        DoctorFragment fragment = new DoctorFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, userId);
        args.putString(ARG_PARAM2, regDate);
        args.putString(ARG_PARAM3, regTypeId);
        args.putString(ARG_PARAM4, departmentId);
        args.putString(ARG_PARAM5, departmentName);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userId = getArguments().getString(ARG_PARAM1);
            regDate = getArguments().getString(ARG_PARAM2);
            regTypeId = getArguments().getString(ARG_PARAM3);
            departmentId = getArguments().getString(ARG_PARAM4);
            departmentName = getArguments().getString(ARG_PARAM5);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_doctor, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mListView = (ListView) rootView.findViewById(R.id.listView);
        mListView.setOnItemClickListener(this);
        //mListView.setAdapter(new ListAdapter(getActivity(), Arrays.asList("1","1","1")));

        HttpRequest.doctorList("doctorList", this, userId, regDate, regTypeId, departmentId);
        adapter = new ListAdapter(getActivity(), null);
        mListView.setAdapter(adapter);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnDoctorFragmentInteractionListener) {
            mListener = (OnDoctorFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Map item = (Map) adapterView.getAdapter().getItem(i);
        if (item.get("doctorId") == null) {
            //ToastUtils.showShort(getActivity(),"HIS数据错误，无号源编码");
            Tools.showDialogWithReturnMainActivity("HIS数据错误，无号源编码，请联系管理员！", getActivity());
            return;
        }
        if (item.get("doctorName") == null) {
            //ToastUtils.showShort(getActivity(),"HIS数据错误，无号源编名称");
            Tools.showDialogWithReturnMainActivity("HIS数据错误，无号源编名称，请联系管理员！", getActivity());
            return;
        }
        if (item.get("totalCost") == null) {
            //ToastUtils.showShort(getActivity(),"HIS数据错误，无挂号费");
            Tools.showDialogWithReturnMainActivity("HIS数据错误，无挂号费，请联系管理员！", getActivity());
            return;
        }
        mListener.onDoctorFragmentInteraction( item.get("doctorId").toString(), item.get("doctorName").toString(), item.get("totalCost").toString());
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            //ToastUtils.showShort(getActivity(),"服务器处理异常");
            Tools.showDialogWithReturnMainActivity("服务器处理异常，请联系管理员！", getActivity());
            return;
        }
        if (!json.isSuccess()) {
            //ToastUtils.showShort(getActivity(),json.getMsg());
            Tools.showDialog(json.getMsg(), getActivity(), clickListener, clickListener);
            return;
        }
        list = (List) json.getData();
        adapter.setList(list, true);
    }

    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        //ToastUtils.showShort(getActivity(),"网络异常");
        Tools.showDialogWithReturnMainActivity("网络连接异常，请联系管理员！", getActivity());
    }

    public interface OnDoctorFragmentInteractionListener {
        void onDoctorFragmentInteraction( String doctorID, String doctorName, String totalCost);

        void onDoctorFragmentJumpPreInteraction();
    }

    class ListAdapter extends CommonAdapter<Map> {

        public ListAdapter(Context context, List<Map> list) {
            super(context, R.layout.item_doctor, list);
        }


        @Override
        public void convert(Wrapper<Map> wrapper, Map item) {
            wrapper.setText(R.id.deptName_tv, departmentName);
            if (item.get("doctorName") == "") {
                wrapper.setText(R.id.doctorName_tv, "默认");
            }else {
                wrapper.setText(R.id.doctorName_tv, (String) item.get("doctorName"));
            }
            wrapper.setText(R.id.scheduleTime_tv, (String) item.get("scheduleTime"));
            wrapper.setText(R.id.limitCount_tv, (String) item.get("limitCount"));
            wrapper.setText(R.id.cost_tv, item.get("totalCost") + " 元");
        }
    }
}
